package communication.client;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client {
    // 以下 是登录窗口的 属性
    private JFrame loginFrame = null;
    private JPanel loginJContentPane = null;
    private JLabel loginlabShowMsg = null; //
    private JLabel loginJlabName = null;
    private JTextField loginTxtName = null;//
    private JButton loginBtnLogin = null;//
    // 以下 是主窗口的 属性
    private JFrame mainFrame = null;
    private JPanel mainJContentPane = null;
    private TextArea mainShowTalk = null;
    private JButton mainBtnSend = null;
    private JLabel mainLabToSay = null;
    private JTextField mainYouInput = null;

    // 初始化 主窗口
    private void initializeMainFrame() {
        mainFrame.setSize(406, 309);
        mainFrame.setResizable(false);
        if (mainJContentPane == null) {
            mainLabToSay = new JLabel();
            mainLabToSay.setBounds(new Rectangle(4, 243, 35, 27));
            mainLabToSay.setText("说吧:");
            mainJContentPane = new JPanel();
            mainJContentPane.setLayout(null);
            if (mainShowTalk == null) {
                mainShowTalk = new TextArea();
                mainShowTalk.setBounds(new Rectangle(4, 2, 395, 235));
                mainShowTalk
                        .setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            }
            mainJContentPane.add(mainShowTalk, null);
            if (mainBtnSend == null) {
                mainBtnSend = new JButton();
                mainBtnSend.setBounds(new Rectangle(328, 243, 66, 24));
                mainBtnSend
                        .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                mainBtnSend.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC,
                        14));
                mainBtnSend.setText("发送");
                mainBtnSend.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //主窗口  点击发送按钮消息
                    }
                });
            }
            mainJContentPane.add(mainBtnSend, null);
            mainJContentPane.add(mainLabToSay, null);
            if (mainYouInput == null) {
                mainYouInput = new JTextField();
                mainYouInput.setText(null);
                mainYouInput.setBounds(new Rectangle(43, 242, 277, 28));
                mainYouInput.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //主窗口  回车 发送按钮消息
                    }
                });
            }
            mainJContentPane.add(mainYouInput, null);
        }
        mainFrame.setContentPane(mainJContentPane);
        mainFrame.setTitle("聊天窗口");
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
//				关闭聊天室窗口
            }
        });
    }

    // 初始化 登录窗口
    private void initializeLoginFrame() {
        if (loginJContentPane == null) {
            loginlabShowMsg = new JLabel();
            loginlabShowMsg.setBounds(new Rectangle(102, 38, 138, 30));
            loginlabShowMsg.setFont(new Font("Dialog", Font.BOLD, 12));
            loginlabShowMsg.setForeground(Color.red);
//			loginlabShowMsg.setText("名字重复,隐姓埋名吧");
            loginlabShowMsg.setText(null);
            loginJlabName = new JLabel();
            loginJlabName.setBounds(new Rectangle(11, 3, 103, 30));
            loginJlabName.setFont(new Font("Dialog", Font.BOLD, 18));
            loginJlabName.setForeground(Color.blue);
            loginJlabName.setText("先报个名吧:");
            loginJContentPane = new JPanel();
            loginJContentPane.setLayout(null);
            loginJContentPane.add(loginJlabName, null);
            if (loginTxtName == null) {
                loginTxtName = new JTextField();
                loginTxtName.setBounds(new Rectangle(139, 2, 87, 33));
                loginTxtName.setFont(new Font("Dialog", Font.PLAIN, 18));
                loginTxtName.setText(null);
                // 向 loginTxtName 添加 动作事件
                loginTxtName.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        // 当 loginTxtName 被敲回车，执行登录操作
//						System.out.println("登录窗口 文本框 敲回车");
                        getExist();
                    }
                });
            }

            if (loginBtnLogin == null) {
                loginBtnLogin = new JButton();
                loginBtnLogin.setBounds(new Rectangle(21, 37, 73, 31));
                loginBtnLogin.setFont(new Font("Dialog", Font.BOLD, 18));
                loginBtnLogin.setText("加入");
                // 向 loginBtnLogin 添加 动作事件
                loginBtnLogin.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        // 当 loginBtnLogin 被敲点击时，执行登录操作
                        System.out.println("点击加入按钮！！！");
                    }
                });
            }
            loginJContentPane.add(loginTxtName, null);
            loginJContentPane.add(loginBtnLogin, null);
            loginJContentPane.add(loginlabShowMsg, null);
        }
        loginFrame.setSize(261, 106);
        loginFrame.setResizable(false);
        loginFrame.setContentPane(loginJContentPane);
        loginFrame.setTitle("欢迎加入弱智聊天室");
        // 添加窗口 关闭事件
        loginFrame.addWindowListener(new WindowAdapter() {
            // 如果 new 监听器(WindowListener) ,必须实现每一个方法
            // 如果 new 适配器(WindowListener) ,需要哪个就实现哪个
            public void windowClosing(WindowEvent e) {
                // 调用 close 方法关闭客户端
            }
        });
    }

    //登录


    //画界面
    private void createUI() {
        loginFrame = new JFrame();
        this.initializeLoginFrame();
        loginFrame.setLocationRelativeTo(null);
        loginFrame.setVisible(true);
        mainFrame = new JFrame();
        this.initializeMainFrame();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(false);
    }

    // 客户端 启动方法
    public void start() {
        // 1、画界面
        this.createUI();
        // 2 、连接服务器
        this.createSocket();
        // 3 、初始化 IO

    }

    private void loginSuccess() {
        mainFrame.setVisible(true);
        loginFrame.setVisible(false);
    }

    private void loginFail() {

    }

    Socket socket;
    BufferedOutputStream bos;
    BufferedInputStream bis;
    byte[] bytes = new byte[1024];
    int len;

    //创建客户端套接字
    private void createSocket() {
        try {
            socket = new Socket("127.0.0.1", 8080);
            bos = new BufferedOutputStream(socket.getOutputStream());
            bis = new BufferedInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //向服务器传输名字
    private void sendUserName() {
        try {
            bos.write(loginTxtName.getText().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //从服务器那里得到数据，判断名字是否存在
    private void getExist() {
        String exist = "";
        byte[] bytes = new byte[1024];
        int len = 0;
        while (true) {
            try {
                while (((len = bis.read(bytes)) != -1)) {
                    exist += new String(bytes, 0, len);
                }
                if (exist.equals("成功")) {
                    loginSuccess();
                } else {
                    loginFail();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}